//
//  TouchExplorerViewController.m
//  TouchExplorer
//
//  Created by TTKai on 11-8-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TouchExplorerViewController.h"

@implementation TouchExplorerViewController
@synthesize messageLabel;
@synthesize tapsLabel;
@synthesize touchesLabel;
@synthesize posLabel;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)dealloc
{
	[messageLabel release];
	[tapsLabel release];
	[touchesLabel release];
	[posLabel release];
	[super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)updateLabelsFromTouches:(NSSet *)touches
{
	NSUInteger numTpas = [[touches anyObject] tapCount];
	NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%d taps detected", numTpas];
	tapsLabel.text = tapsMessage;
	[tapsMessage release];
	
	NSUInteger numTouches = [touches count];
	NSString *touchMsg = [[NSString alloc] initWithFormat:@"%d touches detected", numTouches];
	touchesLabel.text = touchMsg;
	[touchMsg release];
	
	NSMutableString *posMsg = [[NSMutableString alloc] init];
	for(UITouch* touch in touches)
	{
		CGPoint pos = [touch locationInView:self.view];
		[posMsg appendFormat:@" (%.0f %.0f)", pos.x, pos.y];
	}
	UITouch *touch = [touches anyObject];
	CGPoint pos = [touch locationInView:self.view];
	[posMsg appendFormat:@" [%.0f %.0f]", pos.x, pos.y];
	posLabel.text = posMsg;
	[posMsg release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	messageLabel.text = @"Touches Began";
	[self updateLabelsFromTouches:touches];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	messageLabel.text = @"Touches Cancelled";
	[self updateLabelsFromTouches:touches];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	messageLabel.text = @"Touches Stopped.";
	[self updateLabelsFromTouches:touches];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	messageLabel.text = @"Drag Detected";
	[self updateLabelsFromTouches:touches];
}

@end
